package cn.tedu.ivos.vehicle.service.iml;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImp implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<Vehicle> selectVehicleBy(VehicleQuery vehicleQuery) {
        log.debug("开始查询车辆：VehicleQuery={}"+vehicleQuery);
        List<Vehicle> vehicleList = vehicleMapper.selectVehicleBy(vehicleQuery);
//        log.debug("查询结果：{}",vehicleList);
        return vehicleList;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("开始保存车辆：VehicleSaveParam={}"+vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam, vehicle);
        //判断车牌号是否存在
        VehicleVO vehicleVO = vehicleMapper.selectVehicleByLicense(vehicle.getLicense());
        if(vehicle.getId()==null){
            if(vehicleVO!=null){
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setStatus("1");//空闲
            vehicle.setCreateTime(new Date());
            vehicle.setGeofenceBindStatus("0");//未绑定
            vehicleMapper.insertVehicle(vehicle);
        }else{
            if(vehicleVO !=null && !vehicleVO.getId().equals(vehicle.getId())){
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("开始删除车辆：id={}"+id);
        vehicleMapper.deleteVehicleById(id);
    }

    @Override
    public void unBindVehicle(Long vehicleId) {
        log.debug("解绑车辆：vehicleId={}"+vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        /* 之前的update SQL要求围栏id不为null才会拼接更新条件
         * 但此处的围栏id就需要设置为null值,所以复用不了,我们可以写一个新的SQL*/
        vehicleMapper.updateNullGeofenceId(vehicle);
    }

    @Override
    public void bindVehicle(Long geofenceId, Long vehicleId) {
        log.debug("绑定车辆：vehicleId={},geofenceId={}"+vehicleId,geofenceId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceId(geofenceId);
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceBindStatus("1");
        vehicleMapper.updateVehicle(vehicle);
    }
}
